package org.dwpd.controller.blog;

import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dwpd.domain.blog.Blog;
import org.dwpd.domain.blog.BlogComment;
import org.dwpd.domain.common.Result;
import org.dwpd.service.blog.BlogService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Package:org.dwpd.controller
 *
 * @ClassName:BlogController
 * @Create:2024/8/18 16:19
 **/
@RestController
@RequestMapping("blog")
@Slf4j
public class BlogController {
    @Resource
    private BlogService blogService;

    //上传图片
    @PostMapping("upload/images")
    public Result uploadImages(@RequestParam("files") List<MultipartFile> multipartFiles) throws IOException {
        return blogService.uploadImages(multipartFiles);
    }
    //新增博客
    @PostMapping("/saveBlog")
    public Result saveBlog(@RequestBody Blog blog){
        return  blogService.saveBlog(blog);
    }
    //获取博客详情
    @GetMapping("/getBlogDetail/{id}")
    public Result getBlogDetail(@PathVariable("id") Integer id) throws JsonProcessingException {
        return blogService.getBlogDetail(id);
    }
    //评论博客
    @PostMapping("/comment")
    public Result comment(@RequestBody BlogComment blogComment) throws JsonProcessingException {
        return blogService.comment(blogComment);
    }
    //获取博客评论
    @GetMapping("/get/comments/{blogId}")
    public Result getComments(@PathVariable("blogId") Integer id) throws IOException {
        return blogService.getComments(id);
    }

    //删除博客
    @GetMapping("/delete/{id}")
    public Result deleteBlog(@PathVariable("id") Integer id){
        return blogService.deleteBlog(id);
    }

    //删除评论
    @GetMapping("/delete/comment/{id}")
    public Result deleteBlogComment(@PathVariable("id") Integer id){
        return blogService.deleteBlogComment(id);
    }

    //点赞
    @GetMapping("liked/{id}")
    public Result BlogLiked(@PathVariable("id")Integer id){
        return blogService.blogLiked(id);
    }

    //点踩
    @GetMapping("disliked/{id}")
    public Result BlogDisliked(@PathVariable("id")Integer id){
        return blogService.blogDisliked(id);
    }
    //获取博客列表
    @GetMapping("get/blogList/{pageNum}")
    public Result getBlogList(@PathVariable(value = "pageNum") Integer pageNum){
        return blogService.getBlogList(pageNum);
    }

    // 获取指定用户的博客列表
    @GetMapping("getBlogListFromUserId/{userId}")
    public Result getBlogListFromUserId(@PathVariable(value = "userId") Integer userId) {
        return blogService.getBlogListFromUserId(userId);
    }
}
